package ide.ccf.ccdesigner.views;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.*;
import ide.ccf.ccdesigner.views.embedded.WidgetBoxW;

import ide.ccf.ccdesigner.editors.UiEditor;

/**
 * 工具箱视图
 *
 */
public class WidgetBoxView extends ViewPart implements IPartListener 
{
    private WidgetBoxW widgetbox;

    /**
     * 创建Part控制
     */
    public void createPartControl(Composite parent) 
    {
        widgetbox = new WidgetBoxW(parent, SWT.EMBEDDED);
        getSite().getPage().addPartListener(this);
        IEditorPart editor = getSite().getPage().getActiveEditor();
        
        if (!(editor instanceof UiEditor))
            widgetbox.setBoxEnabled(false);
    }

    /**
     * 设置焦点
     */
    public void setFocus() 
    {
        widgetbox.setFocus();
    }

    /**
     * 释放
     */
    public void dispose() 
    {
        getSite().getPage().removePartListener(this);
        super.dispose();
    }

    /**
     * Part激活
     */
    public void partActivated(IWorkbenchPart part) 
    {
        updateWidgetBoxStatus();
    }
    
    /**
     * Part显示到前端
     */
    public void partBroughtToTop(IWorkbenchPart part) 
    {
    	
    }
    
    /**
     * Part关闭
     */
    public void partClosed(IWorkbenchPart part) 
    {
    	
    }
    
    /**
     * Part取消激活
     */
    public void partDeactivated(IWorkbenchPart part) 
    {
        updateWidgetBoxStatus();
    }
    
    /**
     * Part打开
     */
    public void partOpened(IWorkbenchPart part) 
    {
    	
    }

    /**
     * 更新工具箱状态
     */
    private void updateWidgetBoxStatus() 
    {
        IEditorPart editor = getSite().getPage().getActiveEditor();
        
        if (editor instanceof UiEditor) 
        {
            widgetbox.setBoxEnabled(true);
        } 
        else 
        {
            widgetbox.setBoxEnabled(false);
        }
    }
}
